<template>
  <div>
    <el-dialog :title="title" :closeOnClickModal="false" :visible.sync="show" width="650px">
      <el-form label-position="right" :model="selectTable" ref="addForm" label-width="90px" :inline="false" autocomplete="off">
        <el-form-item label="用户名" prop="userAccount" :rules="[rule[0]]">
          <el-input placeholder="请输入" v-model="selectTable.userAccount" maxLength="100"></el-input>
        </el-form-item>
        <el-row>
          <el-col>
            <el-form-item label="头像" prop="avatar">
              <singleImage v-model="selectTable.avatar"></singleImage>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="realName">
              <span>{{selectTable.realName||'暂无'}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <span>{{selectTable.gender||'暂无'}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone" :rules="[rule[0]]">
              <el-input placeholder="请输入" v-model="selectTable.phone" maxLength="100"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input placeholder="请输入" v-model="selectTable.email" maxLength="100"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="地址">
          <el-row>
            <el-col :span="7">
              <el-select v-model="selectTable.province" placeholder="省">
                <el-option value="">请选择</el-option>
                <el-option v-for="(value, key) in division" :key="key" :value="key">{{key}}</el-option>
              </el-select>
            </el-col>
            <el-col :span="7">
              <el-select v-model="selectTable.city" placeholder="市">
                <template v-if="selectTable.province">
                  <el-option value="">请选择</el-option>
                  <el-option v-for="(value, key) in division[selectTable.province]" :key="key" :value="key">{{key}}</el-option>
                </template>
              </el-select>
            </el-col>
            <el-col :span="7">
              <el-select v-model="selectTable.district" placeholder="区">
                <template v-if="selectTable.city">
                  <el-option value="">请选择</el-option>
                  <el-option v-for="(value, key) in division[selectTable.province][selectTable.city]" :key="key" :value="value">{{value}}</el-option>
                </template>
              </el-select>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="介绍" prop="comment">
          <el-input placeholder="请输入" v-model="selectTable.comment" maxLength="100"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as api from '@/utils/api'
import singleImage from '@/components/Upload/singleImage'
import division from '@/utils/division'
export default {
  components: {
    singleImage
  },
  props: {
    data: Object
  },
  created() {
    this.selectTable = this.JSONs(this.data)
  },
  watch: {
    show(newVal) {
      this.$parent.edit = false
    }
  },
  computed: {
    title() {
      if (this.selectTable.method === 'edit') {
        return '修改'
      } else if (this.selectTable.method === 'add') {
        return '添加'
      }
    }
  },
  data() {
    return {
      pload: false,
      division: division,
      rule: [
        { required: true, message: '必填项', trigger: 'change' }
      ],
      show: true,
      selectTable: {}
    }
  },
  methods: {
    async submitForm(formName) {
      await this.$refs[formName].validate()
      try {
        this.pload = true
        if (this.selectTable.method === 'edit') {
          await api.modifyucUser(this.selectTable)
        } else if (this.selectTable.method === 'add') {
          // await api.scheduleSave(this.selectTable)
        }
        this.$notify({ title: '操作成功', type: 'success', duration: 5000 })
        this.$parent.edit = false
        this.$emit('getData')
      } catch (e) {
        this.pload = false
      }
    }
  }
}
</script>
